﻿using System;
using System.Globalization;
namespace YanZhiwei.DotNet2.Utilities.Common
{
    /// <summary>
    /// 时间操作帮助类
    /// </summary>
    public static class TimeHelper
    {
        #region 把秒转换成分钟
        /// <summary>
        /// 把秒转换成分钟
        /// </summary>
        /// <returns>分钟</returns>
        public static int SecondToMinute(int second)
        {
            decimal _minute = (decimal)((decimal)second / (decimal)60);
            return Convert.ToInt32(Math.Ceiling(_minute));
        }
        #endregion
        #region 返回某年某月最后一天
        /// <summary>
        /// 返回某年某月最后一天
        /// </summary>
        /// <param name="year">年份</param>
        /// <param name="month">月份</param>
        /// <returns>日</returns>
        public static int GetMonthLastDate(int year, int month)
        {
            DateTime _lastDay = new DateTime(year, month, new GregorianCalendar().GetDaysInMonth(year, month));
            return _lastDay.Day;
        }
        #endregion
        #region 返回时间差
        /// <summary>
        /// 计算时间差
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns>时间差文字描述</returns>
        public static string DateDiff(DateTime startTime, DateTime endTime)
        {
            string _dateDiff = null;
            TimeSpan _timeSpan = endTime - startTime;
            if (_timeSpan.Days >= 1)
            {
                _dateDiff = startTime.Month.ToString() + "月" + startTime.Day.ToString() + "日";
            }
            else
            {
                if (_timeSpan.Hours > 1)
                {
                    _dateDiff = _timeSpan.Hours.ToString() + "小时前";
                }
                else
                {
                    _dateDiff = _timeSpan.Minutes.ToString() + "分钟前";
                }
            }
            return _dateDiff;
        }
        #endregion
        #region 格式化日期时间
        /// <summary>
        /// 格式化日期时间
        /// </summary>
        /// <param name="dateTime">日期时间</param>
        /// <param name="dateMode">显示模式</param>
        /// <returns>0-9种模式的日期</returns>
        public static string FormatDate(DateTime dateTime, string dateMode)
        {
            switch (dateMode)
            {
                case "0":
                    return dateTime.ToString("yyyy-MM-dd");
                case "1":
                    return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
                case "2":
                    return dateTime.ToString("yyyy/MM/dd");
                case "3":
                    return dateTime.ToString("yyyy年MM月dd日");
                case "4":
                    return dateTime.ToString("MM-dd");
                case "5":
                    return dateTime.ToString("MM/dd");
                case "6":
                    return dateTime.ToString("MM月dd日");
                case "7":
                    return dateTime.ToString("yyyy-MM");
                case "8":
                    return dateTime.ToString("yyyy/MM");
                case "9":
                    return dateTime.ToString("yyyy年MM月");
                default:
                    return dateTime.ToString();
            }
        }
        #endregion
        #region 得到随机日期
        /// <summary>
        /// 得到随机日期
        /// </summary>
        /// <param name="timeStart">起始日期</param>
        /// <param name="timeEnd">结束日期</param>
        /// <returns>间隔日期之间的 随机日期</returns>
        public static DateTime GetRandomTime(DateTime timeStart, DateTime timeEnd)
        {
            Random _random = new Random();
            DateTime _minTime = new DateTime();
            DateTime _maxTime = new DateTime();
            TimeSpan _timeSpan = new TimeSpan(timeStart.Ticks - timeEnd.Ticks);
            // 获取两个时间相隔的秒数
            double _dTotalSecontds = _timeSpan.TotalSeconds;
            int iTotalSecontds = 0;
            if (_dTotalSecontds > Int32.MaxValue)
            {
                iTotalSecontds = Int32.MaxValue;
            }
            else if (_dTotalSecontds < Int32.MinValue)
            {
                iTotalSecontds = Int32.MinValue;
            }
            else
            {
                iTotalSecontds = (int)_dTotalSecontds;
            }
            if (iTotalSecontds > 0)
            {
                _minTime = timeEnd;
                _maxTime = timeStart;
            }
            else if (iTotalSecontds < 0)
            {
                _minTime = timeStart;
                _maxTime = timeEnd;
            }
            else
            {
                return timeStart;
            }
            int maxValue = iTotalSecontds;
            if (iTotalSecontds <= System.Int32.MinValue)
                maxValue = System.Int32.MinValue + 1;
            int i = _random.Next(System.Math.Abs(maxValue));
            return _minTime.AddSeconds(i);
        }
        #endregion
    }
}